package com.firestorm.eagletrons;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;

import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Handler;
import android.view.animation.AnimationUtils;
import android.widget.ImageView;
import android.widget.Toast;

import com.firestorm.eagletrons.login.CustomHttpClient;
import com.firestorm.eagletrons.official.R;

public class SplashActivity extends Activity {
	protected boolean _active = true;
	protected int _splashTime = 2500; // time to display the splash screen in ms
	ImageView imageView;
	ArrayList<NameValuePair> loginInfo;

	@Override
	public void onResume() {
		super.onResume();
		applicationOpen();
	}

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_splash);

		if (!applicationOpen()) {
			Toast.makeText(
					getApplicationContext(),
					"Sorry, the Eagletrons Application is currently closed, or there is a problem connecting to the server.  Please try again later.",
					Toast.LENGTH_LONG).show();
			finish();
		} else {
			Toast.makeText(
					getApplicationContext(),
					"The Eagletrons Application is currently open!",
					Toast.LENGTH_LONG).show();
			SharedPreferences settings = getSharedPreferences("eagletrons", 0);
			Boolean logged_in = settings.getBoolean("logged_in", false);
			if (logged_in) {
				final Handler mHandler = new Handler();
				loginInfo = new ArrayList<NameValuePair>();
				loginInfo.add(new BasicNameValuePair("username", settings
						.getString("username", "null")));
				loginInfo.add(new BasicNameValuePair("password", settings
						.getString("password", "null")));

				final Runnable toast_failed = new Runnable() {
					public void run() {
						Toast toast = Toast
								.makeText(
										getApplicationContext(),
										"Entered username/password incorrect, now logging you out.",
										Toast.LENGTH_SHORT);
						toast.show();
						SharedPreferences settings = getSharedPreferences(
								"eagletrons", 0);
						SharedPreferences.Editor editor = settings.edit();
						editor.putBoolean("logged_in", false);
						editor.putString("username", "null");
						editor.putString("password", "null");
						editor.commit();
					}
				};
				final Runnable toast_success = new Runnable() {
					public void run() {
						Toast toast = Toast.makeText(getApplicationContext(),
								"Successful Login.", Toast.LENGTH_SHORT);
						toast.show();
					}
				};

				Thread thread = new Thread() {
					public void run() {

						// String valid = "1";
						String response = null;
						try {
							response = CustomHttpClient
									.executeHttpPost(
											"http://eagletrons.net/application/android/login.php",
											loginInfo);
							String res = response.toString();
							res = res.replaceAll("\\s+", "");
							if (res.equals("1")) {
								mHandler.post(toast_success);
							} else {
								mHandler.post(toast_failed);
							}
							finish();
							Intent intent = new Intent(
									"EagletronsDashboard.intent.action.Launch");
							startActivity(intent);
						} catch (Exception e) {
							e.printStackTrace();
							finish();
						} finally {

						}

					}

				};

				thread.start();
			} else {
				Thread thread = new Thread() {
					public void run() {

						try {
							Thread.sleep(2500);
						} catch (Exception e) {

						} finally {
							finish();
							Intent intent = new Intent(
									"EagletronsDashboard.intent.action.Launch");
							startActivity(intent);
						}
					}
				};

				thread.start();

			}

			imageView = (ImageView) findViewById(R.id.splash_icon);
			startLoader();
		}
	}

	public void startLoader() {
		imageView.startAnimation(AnimationUtils.loadAnimation(
				SplashActivity.this, R.anim.splash_fadein));
	}

	public boolean applicationOpen() {
		String applicationEnabled = "false";
		try {
			HttpClient client = new DefaultHttpClient();
			HttpGet request = new HttpGet(
					"http://eagletrons.net/application/android/officialopen.conf");
			HttpResponse response = client.execute(request);
			InputStream in = response.getEntity().getContent();
			BufferedReader reader = new BufferedReader(
					new InputStreamReader(in));
			StringBuilder str = new StringBuilder();
			String line = null;
			while ((line = reader.readLine()) != null) {
				str.append(line);
			}
			in.close();
			applicationEnabled = str.toString();
		} catch (Exception e) {
			applicationEnabled = "false";
		}
		return Boolean.parseBoolean(applicationEnabled);
	}
}
